package com.telenav.meishi;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class FoodViewActivity extends Activity {
	private String title = "";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_foodview);
		title = getIntent().getStringExtra("title");
		new AsyncGetDetail().execute(new String[] { "GBK" });
		ProgressBar progress_foodview = (ProgressBar) findViewById(R.id.progress_foodview);
		progress_foodview.setVisibility(View.VISIBLE);
	}

	class AsyncGetDetail extends AsyncTask<String, Integer, Object> {

		@Override
		protected Object doInBackground(String... params) {
			try {
				String t = Util.getFoodMap().get(title);
				File file = new File("/mnt/sdcard/meishi/food" + t + ".zip");
				if (!file.exists()) {
					throw new Exception("Please put detail file to " + file.getAbsolutePath());
				}
				File dir = new File("/mnt/sdcard/meishi/temp");
				if (!dir.exists()) {
					dir.mkdir();
				}
				String indexContent = "";
				ByteArrayOutputStream baos = null;
				ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
				ZipEntry ze = null;
				String zname;
				while ((ze = zis.getNextEntry()) != null) {
					zname = ze.getName();
					if (zname.startsWith(title + File.separator)) {

						baos = new ByteArrayOutputStream();
						byte[] buffer = new byte[1024];
						int length = 0;

						while ((length = zis.read(buffer)) > 0) {
							baos.write(buffer, 0, length);
						}
						zis.closeEntry();

						FileOutputStream fos = new FileOutputStream(dir.getAbsolutePath() + File.separator + zname.substring(zname.indexOf(File.separator) + 1));
						fos.write(baos.toByteArray());
						fos.flush();
						fos.close();

						if (zname.endsWith("index.html")) {
							if (Integer.valueOf(t) > 500) {
								indexContent = new String(baos.toByteArray(), "utf-8");
							} else {
								indexContent = new String(baos.toByteArray(), "GBK");
							}
						}
					}
					zis.closeEntry();
				}
				zis.close();
				return indexContent;
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "";
		}

		@Override
		protected void onPostExecute(Object result) {
			ProgressBar progress_foodview = (ProgressBar) findViewById(R.id.progress_foodview);
			progress_foodview.setVisibility(View.INVISIBLE);
			WebView webView_foodview = (WebView) findViewById(R.id.webView_foodview);
			webView_foodview.loadDataWithBaseURL("file:///mnt/sdcard/meishi/temp/", result.toString(), "text/html", "utf-8", null);
			//				webView_foodview.loadUrl("content://com.android.htmlfileprovider/sdcard/meishi/temp/index.html");

		}
	}

}
